package com.johnzer.frame.app;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.support.multidex.MultiDexApplication;

import java.io.File;

/**
 * Author   : johnzer
 * Data     : 2016-09-06  13:21
 * Describe :
 */
public abstract class BaseApp extends MultiDexApplication {

    private static final String LOGPATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "haobaobeilog";
    private static BaseApp sContext;
    private Handler mMainThreadHandler = null;
    private Looper mMainThreadLooper = null;
    private Thread mMainThread = null;
    protected boolean DEBUG = true;
    private int mMainThreadId;
    private Context sCurruntContext;

    public static BaseApp instance() {
        return sContext;
    }

    @Override
    public final void onCreate() {
        super.onCreate();
        sContext = this;
        this.mMainThreadHandler = new Handler();
        this.mMainThreadLooper = getMainLooper();
        this.mMainThread = Thread.currentThread();
        this.mMainThreadId = Process.myTid();
//        initCrashHandler();
        init();
    }

    public static Context getCurruntContext() {
        return instance().sCurruntContext;
    }

    public static void setCurruntContext(Context curruntContext) {
        instance().sCurruntContext = curruntContext;
    }
    public static Handler getMainThreadHandler() {
        return instance().mMainThreadHandler;
    }

    public static Looper getMainThreadLooper() {
        return instance().mMainThreadLooper;
    }

    public static Thread getMainThread() {
        return instance().mMainThread;
    }

    public static int getMainThreadId() {
        return instance().mMainThreadId;
    }

    public static boolean isDebug(){
        return instance().DEBUG;
    }


    protected abstract void init();

    public void restart() {
        Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getPackageName());
        PendingIntent restartIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC, System.currentTimeMillis() + 50, restartIntent);
        exitProgram();
    }

    public void exitProgram() {
        Process.killProcess(Process.myPid());
        System.exit(10);
    }

}
